import { _decorator, Component, Game, game, instantiate, Node, Prefab, v3 } from 'cc';
import { Army, ArmyState } from '../entity/Army';
import { Soldier } from '../entity/Soldier';
import { UserInfo } from '../common/UserInfo';
import { TowerArmy } from './TowerArmy';
import { MapZone } from '../entity/MapZone';
import WorldManager from '../common/WorldManager';
import { EventManager } from '../event/EventManager';
const { ccclass, property } = _decorator;

@ccclass('TowerController')
export class TowerController extends Component {
    static instance:TowerController

    @property(Prefab)
    towerArmy:Prefab
    @property(Node)
    towerCamera:Node
    @property(Node)
    content:Node

    private soldiersInList: Army[] = []
    private soldiersInNotArrivedList: Army[] = []
    private soldiersOutList: Army[] = []
    private soldiersOutNotArrivedList: Army[] = []

    private myArmy: Army

    protected onLoad(): void {
        TowerController.instance = this
    }

    start() {
        UserInfo.instance.refreshVillageDetail()

        EventManager.instance.on(EventManager.EventType.GetVillageDetail, this.onGetVillageDetail, this)
    
        game.on(Game.EVENT_SHOW, this.onShow, this)
        
    }

    protected onDisable(): void {
        EventManager.instance.off(EventManager.EventType.GetVillageDetail, this.onGetVillageDetail, this)
        game.off(Game.EVENT_SHOW, this.onShow, this)
    }

    onShow(){
        console.log('on show....')
        UserInfo.instance.refreshVillageDetail()
    }

    onGetVillageDetail(){
        this.content.destroyAllChildren()

        this.scheduleOnce(this.init, 0.2)
    }

    async init(){

        
       
        this.myArmy = Army.createArmy(Soldier.createBySoldiersString())
        this.myArmy.state = ArmyState.Stay
        this.myArmy.simZoneId = UserInfo.instance.villageData.zoneId
        this.myArmy.simZoneName = UserInfo.instance.villageData.villageName
        this.myArmy.zoneId = this.myArmy.simZoneId
        this.myArmy.zoneName = UserInfo.instance.villageData.villageName
        this.addArmy(this.myArmy)
        this.setCameraPosition(this.myArmy)
        
        
        for (let item of UserInfo.instance.soldiersInList) {
            let a= Army.createArmyByData(item)
            this.addArmy(a)
        }
        for (let item of UserInfo.instance.soldiersInNotArrivedList) {
            let a= Army.createArmyByData(item)
            this.addArmy(a)
        }

        for (let item of UserInfo.instance.soldiersOutList) {
            let a= Army.createArmyByData(item)
            this.addArmy(a)
        }
        
        for (let item of UserInfo.instance.soldiersOutNotArrivedList) {
            let a= Army.createArmyByData(item)
            this.addArmy(a)
        }
    }

    addArmy(army:Army){
    
        let node = instantiate(this.towerArmy)
        this.content.addChild(node)
        node.getComponent(TowerArmy).setData(army)
    }

    setCameraPosition(myArmy:Army){
        let xy = MapZone.getXyByZoneId(myArmy.zoneId,WorldManager.instance.radius)
        let x = xy[0]
        let y = xy[1]
        this.towerCamera.position = v3(x, y, 0)
    }

}


